package com.ews.onlineexchange.model;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.PostLoad;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import com.ews.onlineexchange.util.SystemUUIDUtil;

/**
 * 实体名称：资质
 * 说明：资质
 * @author 司冰
 *
 */
@Entity
public class QualificationTemplate extends DataEntity implements Comparator<QualificationTemplate> {
	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	
	private List<Sector> sectors = new ArrayList<Sector>();
	private QualificationClass qualificationClass;
	private QualificationLevel qualificationLevel;
	private String             templateIndex;
	private Boolean			   certAuditedResult;
//	private QualificationCert qualificationCert;
	
	@ManyToMany
	public List<Sector> getSectors() {
		return sectors;
	}

	public void setSectors(List<Sector> sectors) {
		this.sectors = sectors;
	}

	@NotNull
	@ManyToOne
	public QualificationClass getQualificationClass() {
		return qualificationClass;
	}

	public void setQualificationClass(QualificationClass qualificationClass) {
		this.qualificationClass = qualificationClass;
	}

	@ManyToOne
	@NotNull
	public QualificationLevel getQualificationLevel() {
		return qualificationLevel;
	}

	public void setQualificationLevel(QualificationLevel qualificationLevel) {
		this.qualificationLevel = qualificationLevel;
	}
	
//	@ManyToOne
//	public QualificationCert getQualificationCert() {
//		return qualificationCert;
//	}
//
//	public void setQualificationCert(QualificationCert qualificationCert) {
//		this.qualificationCert = qualificationCert;
//	}

	@Override
	public int compare(QualificationTemplate o1, QualificationTemplate o2) {
		if (!o1.getQualificationClass().equals(o2.getQualificationClass()))
			throw new RuntimeException("Try to compare between diff QualificationClasses! Check code!!");		
		return new Integer(o1.getQualificationLevel().getValue()).compareTo(o2.getQualificationLevel().getValue());
	}
	
	
	@Transient
	public String getTemplateIndex() {
		return templateIndex;
	}

	public void setTemplateIndex(String templateIndex) {
		this.templateIndex = templateIndex;
	}

	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((qualificationClass == null) ? 0 : qualificationClass.hashCode());
		result = prime * result + ((qualificationLevel == null) ? 0 : qualificationLevel.hashCode());
		result = prime * result + ((sectors == null) ? 0 : sectors.hashCode());
		result = prime * result + ((templateIndex == null) ? 0 : templateIndex.hashCode()) ;
		return result;
	}

	

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		QualificationTemplate other = (QualificationTemplate) obj;
		if (qualificationClass == null) {
			if (other.qualificationClass != null)
				return false;
		} else if (!qualificationClass.equals(other.qualificationClass))
			return false;
		if (qualificationLevel == null) {
			if (other.qualificationLevel != null)
				return false;
		} else if (!qualificationLevel.equals(other.qualificationLevel))
			return false;
		if (sectors == null) {
			if (other.sectors != null)
				return false;
		} else if (!sectors.equals(other.sectors))
			return false;
		if (templateIndex != other.templateIndex)
			return false;
		return true;
	}

	@Transient
	public String getDetail() {
		StringBuilder sb = new StringBuilder();
		sb	.append(getQualificationClass().getName())
			.append("-");
		
		for (Sector sector : sectors) {
			sb	.append(sector.getFullSectorName());
//				.append(";");				
		}
		
		sb	.append("（")
			.append(getQualificationLevel().getAvailableNames())
			.append("）");
		return sb.toString();
	}
	
	@Transient
	public String getSectorNames() {
		StringBuilder sb = new StringBuilder();
		List<Sector> sectorList = this.getSectors();
		for (Sector sector : sectorList) {
			sb.append(sector.getSectorName())
				.append("，");
		}
		if (sb.length() == 0)
			return null;
		return sb.substring(0, sb.length() - 1);
	}
	
	@Transient
	public Set<Sector> getTreeSectors() {
		Set<Sector> resultSet = new TreeSet<>(new Comparator<Sector>() {
			@Override
			public int compare(Sector o1, Sector o2) {
				return o1.getSno().compareTo(o2.getSno());
			}
		});
		for (Sector sector : this.getSectors()) {
			resultSet.addAll(sector.getSectorTree());
		}
		return resultSet;
	}
	
	@PostLoad
	private void postLoad() {
//		super.
		this.getSectors();
		this.setTemplateIndex(SystemUUIDUtil.getUUID());
	}
	@Transient
	public Boolean getCertAuditedResult() {
		return certAuditedResult;
	}

	public void setCertAuditedResult(Boolean certAuditedResult) {
		this.certAuditedResult = certAuditedResult;
	}
	

	
}
